import i18n from 'i18n-js';
import Taro from "@tarojs/taro";
import {
  IProduct,
  networkConfigTypes,
  TNetworkConfigTypeEnu,
  isBLE,
} from '@/config/product';
import { backToRouter } from '@/utils';
import GStatistics, { GStatisticsEvent } from '@/utils/GStatistics';
import SoftApConifg from './configModel/SoftApConifg';
import BTWifiConfig from './configModel/BTWifiConfig';

import { IBaseConfigNetworkProps } from './base/baseConfigNetwork';

export function getConfigModeByNetworkType({
  type,
  config,
}: {
  type: TNetworkConfigTypeEnu;
  config: IProduct;
}) {
  // console.log('getConfigModeByNetworkType', type, config);
  if (isBLE(config)) {
    // 判断是蓝牙控制的，直接返回蓝牙添加
    return null;
  }
  switch (type) {
    case networkConfigTypes.TNetworkConfigTypeAirLink:
      return null;
    case networkConfigTypes.TNetworkConfigTypeAuto:
      return new SoftApConifg({ productConfig: config });
    case networkConfigTypes.TNetworkConfigTypeSoftAp:
      return new SoftApConifg({ productConfig: config });
    case networkConfigTypes.TNetworkConfigTypeBTWifi:
      return new BTWifiConfig({ productConfig: config });
    case networkConfigTypes.TNetworkConfigTypeLan:
      return null;
    case networkConfigTypes.TNetworkConfigTypeNFC:
      return null;
    default:
      break;
  }
  return null;
}

// // 通过产品配置获取首选配网方式
// function getFirstConfigNetworkType(productConfig: IProduct) {
//   const { networkConfigType } = productConfig;
//   if (networkConfigType && networkConfigType.length > 0) {
//     const type = networkConfigType[0];
//     if (type === networkConfigTypes.TNetworkConfigTypeAuto) {
//       return networkConfigTypes.TNetworkConfigTypeAirLink; // Auto的话，就是等于[Airlink,softAp]
//     }
//     return networkConfigType[0];
//   }
//   // 没有配置的话，默认是softAp
//   return networkConfigTypes.TNetworkConfigTypeSoftAp;
// }

// 通过配置和一种配网方式，来获取另外一种配网方式
function getOtherConfigNetworkType(
  productConfig: IProduct,
  current: TNetworkConfigTypeEnu
) {
  if (isBLE(productConfig)) {
    // 判断是蓝牙控制的，暂不支持与其他配网方式共存
    return null;
  }
  const { networkConfigType } = productConfig;
  if (networkConfigType && networkConfigType.length > 0) {
    let types = networkConfigType;
    if (types.includes(networkConfigTypes.TNetworkConfigTypeAuto)) {
      types = [
        networkConfigTypes.TNetworkConfigTypeAirLink,
        networkConfigTypes.TNetworkConfigTypeSoftAp,
      ];
    }
    const type = types.find((i: TNetworkConfigTypeEnu) => i !== current);
    return type;
  }
  return null;
}

// // 通过productKey来获取配网的model
// export function getConfigNetworkModelByProductConfig(productConfig: IProduct) {
//   if (productConfig) {
//     const type = getFirstConfigNetworkType(productConfig);
//     return getConfigModeByNetworkType({ type, config: productConfig });
//   }
//   return null;
// }

// 通过配网model来获取第二张配网方式
export function getOtherConfigNetworkModel(
  configModel: IBaseConfigNetworkProps
) {
  if (configModel && configModel.productConfig) {
    const type = getOtherConfigNetworkType(
      configModel.productConfig,
      configModel.type
    );
    if (type) {
      return getConfigModeByNetworkType({
        type,
        config: configModel.productConfig,
      });
    }
  }
  return null;
}

export function getConfigModelName(configModel: IBaseConfigNetworkProps) {
  switch (configModel.type) {
    case networkConfigTypes.TNetworkConfigTypeSoftAp:
      return i18n.t('ADD_BY_STEPS');
    case networkConfigTypes.TNetworkConfigTypeBTWifi:
      return i18n.t('ADD_BY_BT');
    default:
      break;
  }
  return null;
}

// 检查当前配网模式的条件，手机是否支持
export async function checkConfigModelIsSupport({
  configModel,
  showToast = true,
}: {
  configModel: IBaseConfigNetworkProps;
  showToast?: boolean;
}) {
  if (configModel.type === networkConfigTypes.TNetworkConfigTypeNFC
    || configModel.type === networkConfigTypes.TNetworkConfigTypeAirLink
    || configModel.type === networkConfigTypes.TNetworkConfigTypeLan
    || configModel.type === networkConfigTypes.TNetworkConfigTypeBle
  ) {
    return false;
  }
  return true;
}

export function needUserCheckStep(configModel: IBaseConfigNetworkProps) {
  return configModel && configModel.type === networkConfigTypes.TNetworkConfigTypeBTWifi;
}

export function needShowReset(configModel: IBaseConfigNetworkProps) {
  return configModel && configModel.type === networkConfigTypes.TNetworkConfigTypeBTWifi;
}

export function backToHome() {
  backToRouter('pages/home/index');
}

export function goToSuccess() {
  Taro.redirectTo({ url: '/pages/configDevice/configSuccess/index' });
}

export function goToFail(noShowRetry = false) {
  if (noShowRetry) {
    Taro.navigateTo({ url: `/pages/configDevice/failTip/index?noRetry=1` });
  } else {
    Taro.redirectTo({ url: `/pages/configDevice/failTip/index?noRetry=0` });
  }
}

interface IPostConfigResProps {
  productKey: string,
  success: boolean,
  res: any,
  startMoment: moment.Moment
}

export function postConfigRes({ productKey, type, success, res, startMoment }: IPostConfigResProps & { type: TNetworkConfigTypeEnu }) {
  GStatistics.postCalculateEvent(new GStatisticsEvent.DeviceConfigNetWorkEvent(
    {
      extra: {
        product_key: productKey,
        network_type: type,
        config_success: success,
        config_result: JSON.stringify(res),
        time_diff: GStatistics.timeDiffToNow(startMoment),
      }
    }));
}

export function postBleConfigRes(p: IPostConfigResProps) {
  postConfigRes({ ...p, type: networkConfigTypes.TNetworkConfigTypeBTWifi });
}

export function postSoftApConfigRes(p: IPostConfigResProps) {
  postConfigRes({ ...p, type: networkConfigTypes.TNetworkConfigTypeSoftAp });
}
